/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.postorder.flow.craete;

import com.lxk.event.helper.EventHelper;
import com.lxk.event.oms.OrderOptionLogEvent;
import com.lxk.oms.order.domain.vo.OmsOrderVo;
import com.lxk.oms.postorder.domain.bo.OmsPostOrderBo;
import com.lxk.oms.postorder.domain.bo.OmsPostOrderItemBo;
import com.lxk.oms.postorder.service.IOmsPostOrderItemService;
import com.lxk.oms.postorder.service.IOmsPostOrderService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.ValidationUtils;
import org.dromara.common.core.validate.AddGroup;

import java.util.List;

import static com.lxk.oms.postorder.flow.craete.FlowPostOrderNodeConst.POST_ORDER_CREATE_NODE;

/**
 * 售后订单创建
 *
 * @author heyi
 * 2024/9/28
 */
@Slf4j
@LiteflowComponent(value = POST_ORDER_CREATE_NODE)
@AllArgsConstructor
public class PostOrderCreateNode extends NodeComponent {
    private final IOmsPostOrderService postOrderService;
    private final IOmsPostOrderItemService postOrderItemService;


    @Override
    public void process() throws Exception {
        PostOrderCreateContext context = this.getContextBean(PostOrderCreateContext.class);
        OmsPostOrderBo postOrderBo = context.getPostOrderBo();
        OmsOrderVo omsOrder = context.getOmsOrder();
        ValidationUtils.validate(postOrderBo, AddGroup.class);
        Boolean b = postOrderService.insertByBo(postOrderBo);
        if (b) {
            Long postOrderBoId = postOrderBo.getId();
            List<OmsPostOrderItemBo> postOrderItemBoList = context.getPostOrderItemBoList();
            postOrderItemBoList.forEach(item -> {
                item.setPostOrderId(postOrderBoId);
                ValidationUtils.validate(item, AddGroup.class);
                postOrderItemService.insertByBo(item);
            });
            context.setResult(new PostOrderCreateContext.CreatePostOrderResult(postOrderBo, postOrderItemBoList, omsOrder));
        }
    }

    @Override
    public void onSuccess() throws Exception {
        PostOrderCreateContext context = this.getContextBean(PostOrderCreateContext.class);
        PostOrderCreateContext.CreatePostOrderResult result = context.getResult();
        // 发送事件
        OrderOptionLogEvent createEvent = new OrderOptionLogEvent();
        OmsPostOrderBo postOrderBo = result.getPostOrderBo();
        createEvent.setOrderSn(postOrderBo.getPostOrderNo());
        createEvent.setUserId(postOrderBo.getUserId());
        createEvent.setUserType(context.getUserType());
        createEvent.setMerId(postOrderBo.getMerId());
        createEvent.setOrderLogEnums(OrderOptionLogEvent.OrderLogEnums.POST_ORDER);
        createEvent.setContent(OrderOptionLogEvent.PostOrderContentFormatEnums.CREATE_POST_ORDER.formatContent(postOrderBo.getOrderSn(),
            postOrderBo.getPostOrderNo(), postOrderBo.getExplains()));
        EventHelper.publishEvent(createEvent);
    }
}
